package com.poseidon.prototype;

import java.io.IOException;
import java.net.URL;

import com.jme.bounding.OrientedBoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jmex.model.collada.ColladaImporter;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.TranslationalJointAxis;

public class Engine extends Node {

    private static final long serialVersionUID = 1L;

    Vector3f offset;
    static int numberOfEngines = 0;
    private DynamicPhysicsNode dynamicEngineNode;
    private TranslationalJointAxis tractionAxis;
    private int rotation;
    private Physics physics;

    public Engine( final DynamicPhysicsNode dynamicEngineNode, Vector3f offset,int rotation, final PhysicsSpace pSpace, Physics physics) {
        super( "engine" );
        this.dynamicEngineNode = dynamicEngineNode;
        this.physics= physics;
        this.rotation=rotation;
        numberOfEngines++;
        createEngine(rotation,pSpace);
        //this.attachChild(node);
	}

    private void createEngine(int rotation,final PhysicsSpace pSpace){
        createObject(dynamicEngineNode,"engine","jmetest/data/model/falcon_thruster.dae",pSpace);
	}

  /*
   * Arvanitidis Georgios 2009/11/25
   * createObject(DynamicPhysicsNode object,String name, String pathToFile, final PhysicsSpace pSpace);
   * this method loads the COLLADA model from the input directory.
   * The COLLADA fire provide various information, like the 3D graphics.
   */
  private void createObject(DynamicPhysicsNode object,String name, String pathToFile, final PhysicsSpace pSpace){

        URL model = Robot.class.getClassLoader().getResource(pathToFile);
        System.out.println("Engines"+numberOfEngines);
        try {
            ColladaImporter.load(model.openStream(), name);
            Node myModel = (Node) ColladaImporter.getModel();
            ColladaImporter.cleanUp();

            myModel.setModelBound(new OrientedBoundingBox());
            myModel.updateModelBound();
            myModel.setLocalScale(.08f);

            //Attach the file model to the dynamic node
            object.setName(name);
            object.attachChild(myModel);

            //Generate the physics
            object.generatePhysicsGeometry();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }
        this.attachChild(object);
	}

  public Vector3f getCenter(){
   return dynamicEngineNode.getLocalTranslation();
  }


  public void activation(int direction, Vector3f distance, double power){
     float force= (float) power;
     if(direction==2){
         dynamicEngineNode.addForce(new Vector3f(0,force,0));
     }
     else if(direction==-2){
         dynamicEngineNode.addForce(new Vector3f(0,-force,0));
     }
     else {
         float forceX= rotation*direction*force* distance.z;
         float forceY= -10* distance.y*distance.x/distance.z;
         float forceZ= -rotation*direction*force* distance.x;
         Vector3f powerVector=new Vector3f(forceX,forceY,forceZ);
         dynamicEngineNode.addForce(powerVector);
     }
  }


  public void physicsForces(){
    float signX;
    float signY;
    float signZ;

    if (dynamicEngineNode.getLinearVelocity(null).x<0) {signX=-1;}
    else signX=1;
    Float x = new Float(Math.abs(dynamicEngineNode.getLinearVelocity(null).x));
    physics.calcolateFriction(x.doubleValue());
    double frictionX= physics.getFrictionPush();


    if (dynamicEngineNode.getLinearVelocity(null).y<0) {signY=-1;}
    else signY=1;
    Float y = new Float(Math.abs(dynamicEngineNode.getLinearVelocity(null).y));
    physics.calcolateFriction(y.doubleValue());
    double frictionY= physics.getFrictionPush();


    if (dynamicEngineNode.getLinearVelocity(null).z<0) {signZ=-1;}
    else signZ=1;
    Float z = new Float(Math.abs(dynamicEngineNode.getLinearVelocity(null).z));
    physics.calcolateFriction(z.doubleValue());
    double frictionZ= physics.getFrictionPush();
    Vector3f prova=new Vector3f(signX*(float)frictionX,signY*(float)frictionY,signZ*(float)frictionZ);
    dynamicEngineNode.addForce(prova);
  };

}